package com.gjintel.framework.log.controller;

import java.util.Date;
import java.util.Iterator;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.gjintel.framework.cache.InitCacheData;
import com.gjintel.framework.constant.Constant;
import com.gjintel.framework.dict.model.Dictionary;
import com.gjintel.framework.log.model.LogInfo;
import com.gjintel.framework.log.service.ILogInfoService;
import com.gjintel.framework.util.CommUtil;
import com.gjintel.framework.util.DictUtil;
import com.gjintel.sdk.base.dao.Page;
import com.gjintel.sdk.base.dao.Sort;

@RequestMapping("/framework/log")
@Controller
public class LogController {

	@Autowired
	private ILogInfoService logInfoService;

	/**
	 * @方法名：list
	 * @功能说明：根据查询条件获得消息类型记录
	 * @author fengya
	 * @date 2014-5-30 下午04:39:36
	 * @return void
	 */
	@RequestMapping("/list")
	@SuppressWarnings("unchecked")
	@ResponseBody
	public JSONObject list(Page page, LogInfo log, Date startTime, Date endTime, Sort sort) {
		this.logInfoService.findAll(page, log, sort, startTime, endTime);
		Iterator<LogInfo> it = page.getResult().iterator();
		LogInfo temp = null;
		Dictionary dict = null;
		while (it.hasNext()) {
			temp = it.next();
			dict = DictUtil.dict("log_op_type", temp.getOpType());
			if (dict != null) {
				temp.setOpType(dict.getName());
			}
		}
		return CommUtil.getPageJSONData(page, true);
	}

	/**
	 * @方法名：remove
	 * @功能说明：根据选择的记录进行删除数据
	 * @author fengya
	 * @date 2014-4-17 下午04:13:04
	 * @return void
	 * @throws Exception
	 */
	@RequestMapping("/remove")
	@ResponseBody
	public String remove(String deleteIds) throws Exception {
		this.logInfoService.removeData(deleteIds);
		InitCacheData.getInstance().initDeptData();
		return Constant.DEL_SUCCESS;
	}
	
	
	/**
	 *  @description 显示日志内容
	 *  @author shy
	 *  @date 2017-1-4
	 */
	@RequestMapping("/contentPage")
	public ModelAndView contentPage(String id){
		ModelAndView mv = new ModelAndView();
		mv.addObject("logId", id);
		mv.setViewName("framework/log/log-content");
		return mv;
	}
	
	/**
	 *  @description 根据id 获取日志信息
	 *  @author shy
	 *  @date 2017-1-4
	 */
	@RequestMapping("/logContent")
	@ResponseBody
	public String getLogContent(String logId){
		String content ="";
		LogInfo logInfo  = this.logInfoService.getLogInfoById(logId);
		if(logInfo !=null){
			content = logInfo.getLogContent();
		}
		return content;
	}
	
}
